package com.mentaleye.controller;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.AutoFormDataResourceBean;
import com.mentaleye.biz.AutoFormDataSourceBiz;
import com.mentaleye.biz.CommonBizImpl;
import com.mentaleye.biz.DictionaryBiz;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 数据源
 */
@Controller
@RequestMapping(value = "/dataSource")
public class AutoFormDataSourceController {

    @Autowired
    private DictionaryBiz dictionaryBiz;
    @Autowired
    private AutoFormDataSourceBiz autoFormDataSourceBiz;
    @RequestMapping(value = "/list")
    public ModelAndView list() {
        ModelAndView view = new ModelAndView("cm/autoFormDataSource/autoFormDataSourceList");
//        Map<Integer,String> classTypeMap=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.CURRICULUM_TYPE.getCode());
//        Map<Integer,String>   classStep=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.CURRICULUM_STEP.getCode());
//        view.addObject("classTypeMap", classTypeMap);
//        view.addObject("classStepMap", classStep);
//        view.addObject("classType",  JSONObject.toJSONString(classTypeMap));
//        view.addObject("classStep",  JSONObject.toJSONString(classStep));
        return view;
    }

    @RequestMapping("/listData")
    @ResponseBody
    public String listData( CommonPage commonPage) {
        CommonBizImpl.queryByPage("AutoFormDataResource",commonPage);
        return commonPage.toSHowPage();
    }

    @RequestMapping({"/edit"})
    public ModelAndView edit(@RequestParam(value="id", required=false) Integer id) {
        ModelAndView view = new ModelAndView("cm/autoFormDataSource/autoFormDataSourceEdit");
        if (id != null) {
            view.addObject("autoFormDataSource",
                    CommonBizImpl.getBeanById("AutoFormDataResource","id",id,true));
        }
        return view;
    }

    @RequestMapping(value = "/save")
    public @ResponseBody String save( AutoFormDataResourceBean autoFormDataResourceBean) {
        return JSONObject.toJSONString(CommonBizImpl.edit(autoFormDataResourceBean,"AutoFormDataResource","id","dataStatus"));
    }

    @RequestMapping(value = "/delete")
    public @ResponseBody String delete(@RequestBody List<Integer> ids) {
        return JSONObject.toJSONString(CommonBizImpl.delete("AutoFormDataResource",ids,"id","dataStatus"));
    }

    @RequestMapping(value = "/explanSql")
    public @ResponseBody
    CommonResult explanSql(String explanSql) {
        return  autoFormDataSourceBiz.explanSql(explanSql);//JSONObject.toJSONString(CommonBizImpl.edit(autoFormDataResourceBean,"AutoFormDataResource","id"));
    }

}
